using UnityEditor;
using UnityEngine.UI;
using UnityEngine;
using UnityEditor.SceneManagement;
using UnityEditor.Experimental.SceneManagement;

namespace SgsUI.Editor{
    public static class UI{
        private const string LayerName = "UI";

        static public GameObject CreateButton()
        {
            GameObject go = UIUtility.CreateUIElementRoot("button",typeof(SpriteRes),typeof(ButtonRes),typeof(ViewButton));
            return go;
        }

        static public GameObject CreateLabel()
        {
            GameObject go = UIUtility.CreateUIElementRoot("label",typeof(LabelRes),typeof(ViewLabel));
            return go;
        }

        static public GameObject CreateNode()
        {
            GameObject go = UIUtility.CreateUIElementRoot("node",typeof(ViewNode));
            return go;
        }

        static public GameObject CreateSprite()
        {
            GameObject go = UIUtility.CreateUIElementRoot("sprite",typeof(ViewSprite));
            return go;
        }
        static public GameObject CreateNewUI()
        {
            // Root for the UI
            var root = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler),typeof(ViewCanvas));
            root.layer = LayerMask.NameToLayer(LayerName);
            Canvas canvas = root.GetComponent<Canvas>();
            canvas.renderMode = RenderMode.ScreenSpaceCamera;
            var camera = GameObject.Find("Main Camera");
            canvas.worldCamera = camera.GetComponent<Camera>();

            CanvasScaler canvasScaler = root.GetComponent<CanvasScaler>();
            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ConstantPixelSize;

            // Works for all stages.
            StageUtility.PlaceGameObjectInCurrentStage(root);

            PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
            if (prefabStage != null)
            {
                Undo.SetTransformParent(root.transform, prefabStage.prefabContentsRoot.transform, "");
            }

            Undo.SetCurrentGroupName("Create " + root.name);

            return root;
        }

        static public Sprite GetSprite(string path)
        {
            return Resources.Load<Sprite>(path);
        }
    }
}